package com.farm.shop.controller;

import cn.hutool.core.convert.Convert;
import com.farm.common.annotation.Log;
import com.farm.common.utils.PageUtils;
import com.farm.common.utils.Query;
import com.farm.common.utils.R;
import com.farm.shop.constant.PageParam;
import com.farm.shop.domain.ScenicinforDO;
import com.farm.shop.service.ScenicinforService;
import com.farm.utils.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 景区咨询
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-15 16:02:24
 */

@Controller
@RequestMapping("/farm/scenicinfor")
public class ScenicinforController {
    @Autowired
    private ScenicinforService scenicinforService;

    /**
     * app 获取咨询详情
     *
     * @param params 请求参数
     * @return Result
     */
    @Log(value = "A获取咨询详情")
    @ResponseBody
    @GetMapping("/app/scenicinfordetail")
    public Result getScenicinforsDetail(@RequestParam Map<String, Object> params) {

        Integer scenicinfoId = Convert.convert(Integer.class, params.get("scenicinfoId"));
        ScenicinforDO appScenicinforsDetail = scenicinforService.getScenicinforsDetail(scenicinfoId);
        return Result.success(appScenicinforsDetail);
    }

    /**
     * app 景区资讯  分页
     *
     * @param params 请求参数
     * @return Result
     */
    @Log(value = "A获取景区资讯列表")
    @ResponseBody
    @GetMapping("/app/scenicinfors")
    public Result getScenicinfors(@RequestParam Map<String, Object> params) {

        int currPage = Integer.parseInt((String) params.get("page"));
        params.put("offset", (currPage - 1) * PageParam.LIMIT);
        params.put("limit", PageParam.LIMIT);

        Query query = new Query(params);

        List<ScenicinforDO> appScenicinfors = scenicinforService.getScenicinforList(query);
        int total = scenicinforService.count(query);
        PageUtils pageUtils = new PageUtils(total, currPage, appScenicinfors);
        return Result.success(pageUtils);
    }

    @GetMapping()
    @RequiresPermissions("farm:scenicinfor:scenicinfor")
    String Scenicinfor() {
        return "farm/scenicinfor/scenicinfor";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("farm:scenicinfor:scenicinfor")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScenicinforDO> scenicinforList = scenicinforService.list(query);
        int total = scenicinforService.count(query);
        PageUtils pageUtils = new PageUtils(scenicinforList, total);
        return pageUtils;
    }

    @Log(value = "B模糊查询资讯列表")
    @ResponseBody
    @GetMapping("/searchTitle")
    @RequiresPermissions("farm:scenicinfor:scenicinfor")
    public PageUtils searchTitle(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ScenicinforDO> scenicinforList = scenicinforService.searchTitle(query);
        int total = scenicinforService.searchTitleCount(query);
        PageUtils pageUtils = new PageUtils(scenicinforList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("farm:scenicinfor:add")
    String add() {
        return "farm/scenicinfor/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("farm:scenicinfor:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        ScenicinforDO scenicinfor = scenicinforService.get(id);
        model.addAttribute("scenicinfor", scenicinfor);
        return "farm/scenicinfor/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("farm:scenicinfor:add")
    public R save(ScenicinforDO scenicinfor) {
        if (scenicinforService.save(scenicinfor) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("farm:scenicinfor:edit")
    public R update(ScenicinforDO scenicinfor) {
        scenicinforService.update(scenicinfor);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("farm:scenicinfor:remove")
    public R remove(Integer id) {
        if (scenicinforService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("farm:scenicinfor:batchRemove")
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        scenicinforService.batchRemove(ids);
        return R.ok();
    }

}
